/*
 * Copyright (c) 2009 by ZDO Corporation.
 * Author: A. Onur Cinar
 * 
 * http://code.google.com/p/zdo-struts2
 * http://www.zdo.com
 * 
 * This file is part of ZDO Struts 2 Applications.
 *
 * ZDO-Struts2 is free software: you can redistribute it and/or
 * modify it under the terms of  the GNU General Public License
 * as published by the Free Software Foundation, either version
 *  3 of the License, or any later version.
 *
 * ZDO-Struts2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

package com.zdo.components;

import java.io.Serializable;

/**
 * Pagination bean.
 */
public class PaginationBean implements Serializable
{
	private static final long serialVersionUID = -8928974736068422017L;

	/** Page number. */
	private int page;
	
	/** Total count of items. */
	private int count;
	
	/** Max items in page. */
	private int max;

	/**
	 * Gets the page number.
	 * 
	 * @return page number.
	 */
	public int getPage ()
	{
		return page;
	}
	
	/**
	 * Sets the page number.
	 * 
	 * @param page page number.
	 */
	public void setPage (int page)
	{
		this.page = page;
	}

	/**
	 * Gets the total count of items.
	 * 
	 * @return items count.
	 */
	public int getCount ()
	{
		return count;
	}

	/**
	 * Sets the total count of items.
	 * 
	 * @param count items count.
	 */
	public void setCount (int count)
	{
		this.count = count;
	}
	
	/**
	 * Gets the max items in page.
	 * 
	 * @return max items.
	 */
	public int getMax ()
	{
		return max;
	}
	
	/**
	 * Sets the max items in page.
	 * 
	 * @param max max items.
	 */
	public void setMax (int max)
	{
		this.max = max;
	}
	
	/**
	 * Gets the total number of pages.
	 * 
	 * @return total pages.
	 */
	public int getTotal ()
	{
		return (int) Math.ceil((double) count / max);
	}
	
	/**
	 * Gets the index of first item.
	 * 
	 * @return first item.
	 */
	public int getFirst ()
	{
		return Math.max((page - 1) * max, 0);
	}

	/**
	 * Gets the index of last item.
	 * 
	 * @return last item.
	 */
	public int getLast ()
	{
		return Math.min(getFirst() + max, count);
	}
}
